// 引用用户模版数据
const Films = require('../models/Films.js');
const tags = require('../tags')

const filmsCon = {
    // 电影显示
    show_films: async function (req, res, next) {
        let selectpage = Number(req.query.page)
        if (isNaN(selectpage)) {
            selectpage = Math.floor((Math.random() * 100) + 1)
        }
        const offset = (selectpage - 1) * 6;
        try {
            let films = await Films.get_films(offset)
            let jfilms = JSON.parse(JSON.stringify(films))
            res.render('index', {
                'jfilms': jfilms,
                'selectpage': selectpage,
                'login': req.session.login,
                'username': req.session.username,
                'nav': "首页"
            })
        } catch (e) {
            res.json({
                code: 0,
                message: "操作失败",
                data: e
            })
        }
    },
    show_tags: async function (req, res, next) {
        var selectpage = Number(req.query.page)
        if (req.query.tags != undefined) {
            req.session.tag = req.query.tags
            var selectpage = 1;
        }
        const offset = selectpage * 6
        try {
            let films = await Films.get_tags_films(req.session.tag, offset)
            let jfilms = JSON.parse(JSON.stringify(films))
            res.render('tags', {
                'jtags': tags,
                'selected': req.session.tag,
                'jfilms': jfilms,
                'selectpage': selectpage,
                'login': req.session.login,
                'username': req.session.username,
                'nav': "分类"
            })
        } catch (e) {
            res.json({
                code: 0,
                message: "操作失败",
                data: e
            })
        }
    },
    search: async function (req, res, next) {
        if (req.body.name == undefined) {
            req.session.name = ''
        } else {
            req.session.name = req.body.name
        }
        try {
            var films = await Films.search(req.session.name)
            const jfilms = JSON.parse(JSON.stringify(films))
            res.render('search', {
                'jfilms': jfilms,
                'value': req.session.name,
                'login': req.session.login,
                'username': req.session.username,
                'nav': "搜索"
            })
        } catch (e) {
            res.json({
                code: 0,
                message: "操作失败",
                data: e
            })
        }
    },
    // rank_list
    rankList: async function (req, res) {
        if (req.session.login) {
            let selectpage = Number(req.query.page)
            if (isNaN(selectpage)) {
                selectpage = 1
            }
            const offset = (selectpage - 1) * 6;

            const uid = req.session.uid;
            const rank_list = await Films.rankList(uid, offset)
            let jfilms = JSON.parse(JSON.stringify(rank_list));
            
            res.render('rank', {
                jfilms: jfilms,
                selectpage: selectpage,
                login: req.session.login,
                username: req.session.username,
                nav: '评分',
            })
        } else {
            res.render('rank', {
                jfilms: {},
                nav:'评分'
            })
        }
    }


}

module.exports = filmsCon;